package com.raidroid.rdwhois;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ManageServer extends Activity{

	int _id = 0;
		
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.editserver);
		
		TextView labelTitle = (TextView) findViewById(R.id.EditBoxTitle);
		labelTitle.setText(getString(R.string.Add_New_Whois_Server));
		
		Bundle extras = getIntent().getExtras(); 
		if(extras != null)
		{
			_id = extras.getInt("server_id");
			labelTitle.setText(getString(R.string.Edit_Whois_Server));
		}
		
		final EditText txtServerName = (EditText) findViewById(R.id.TxtServerName);
		final EditText txtServerAddress = (EditText) findViewById(R.id.TxtServerAddress);
		final EditText txtServerPort = (EditText) findViewById(R.id.TxtServerPort);
		final EditText txtServerDescription = (EditText) findViewById(R.id.TxtServerDescription);
		
		if(_id != 0){		
			DBAdapter db = new DBAdapter(this);
	        db.open();
			Cursor c = db.getServer(_id);
			if (c.moveToFirst())
	        {
	            txtServerName.setText(c.getString(1));
	            txtServerAddress.setText(c.getString(2));
	            txtServerPort.setText(c.getString(3));
	            txtServerDescription.setText(c.getString(4));
	        }
			db.close();			
		}		
		
		Button btnSave = (Button) findViewById(R.id.EditBoxSave);
		btnSave.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				String serverName = txtServerName.getText().toString().trim();
				String serverAddress = txtServerAddress.getText().toString().trim();
				String serverPort = txtServerPort.getText().toString().trim();
				String serverDescription = txtServerDescription.getText().toString().trim();
				
				if(serverName.equals("") 
						|| serverAddress.equals("") 
						|| serverPort.equals("") 
						|| serverDescription.equals(""))
				{
					Toast.makeText(ManageServer.this, 
							getString(R.string.All_fields_are_reguired), 
							Toast.LENGTH_LONG).show();
				}
				else
				{	
					int sPort = 43;
					try{
						sPort = Integer.parseInt(serverPort);
					}catch(Exception e){
						Toast.makeText(ManageServer.this, 
								getString(R.string.Server_Port_must_be_numeric), 
								Toast.LENGTH_LONG).show();
						return;
					}
					
					if(_id != 0){		
						DBAdapter db = new DBAdapter(ManageServer.this);
				        db.open();			        
				        db.updateServer(_id, serverName, serverAddress, sPort, serverDescription);
				        db.close();
				        ManageServer.this.finish();
					}else{
						DBAdapter db = new DBAdapter(ManageServer.this);
				        db.open();			        
				        db.insertServer(serverName, serverAddress, sPort, serverDescription);
				        db.close();
				        ManageServer.this.finish();
					}
				}
				
			}
		});
		
		Button btnCancel = (Button) findViewById(R.id.EditBoxCancel);
		btnCancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ManageServer.this.finish();
			}
		});
	}
}
